package com.tuanzi.loan.web.bean.family.largeamount;

import com.tuanzi.loan.business.entity.family.LoanLargeNewDelivery;
import com.tuanzi.loan.business.entity.family.LoanWholeSaleComment;
import com.tuanzi.loan.business.service.family.LargeNewDeliveryService;
import com.tuanzi.loan.business.service.family.LoanLargeAmountPlusRecordService;
import com.tuanzi.loan.business.service.family.LoanWholeSaleCommentService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.family.largeamount.LargeNewDeliveryRequest;
import com.tuanzi.loan.vo.response.family.largeamount.LargeNewDeliveryResponse;
import com.tuanzi.loan.vo.view.family.CommRegionNewInfo;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.family.largeAmount.LargeNewDeliveryListLazyDataModel;
import com.tuanzi.server.loan_base.constant.LoanRedisKey;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;

/**
 * User: liangZongXi
 * Create time: 10:48 2019/7/9
 * Description:
 */
@Data
@Slf4j
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LargeNewDeliveryListBean extends JSFListingBean<LargeNewDeliveryRequest, LargeNewDeliveryResponse> {

    private static final long serialVersionUID = -7368847161702961227L;

    private List<LoanWholeSaleComment> loanWholeSaleCommentList;
    private List<CommRegionNewInfo> commRegionNewInfoList;

    @Autowired
    private LoanWholeSaleCommentService loanWholeSaleCommentService;
    @Autowired
    private LoanLargeAmountPlusRecordService loanLargeAmountPlusRecordService;
    @Autowired
    private LargeNewDeliveryService largeNewDeliveryService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostConstruct
    private void init(){
        $ = new LargeNewDeliveryRequest();
        lazyDataModel = new LargeNewDeliveryListLazyDataModel($);
        initRequest();
    }

    private void initRequest(){
        if(null == loanWholeSaleCommentList){
            loanWholeSaleCommentList = loanLargeAmountPlusRecordService.getWaiTuiProduct();
        }
        if(null == commRegionNewInfoList){
            commRegionNewInfoList = loanWholeSaleCommentService.findAllRegionInfo();
        }
    }

    public void onReset(){
        init();
    }

    public String toAdd(){
        return redirect("/views/family/WholeSaleManage/largeNewDeliveryInfo.xhtml");
    }

    public String onView(Long id){
        return redirect("/views/family/WholeSaleManage/largeNewDeliveryInfo.xhtml?id="+id);
    }

    public void delete(Long id){
        if(null != id){
            LoanLargeNewDelivery one = largeNewDeliveryService.findOne(id);
            if(null != one){
                largeNewDeliveryService.delete(one);
                stringRedisTemplate.delete(LoanRedisKey.LOAN_LARGE_OUTER_NEW_DELIVERY_LIST);
                addSuccessfullyMessage();
            }
        }

       init();
    }

}
